/*
 * @lc app=leetcode id=67 lang=cpp
 *
 * [67] Add Binary
 */
class Solution {
public:
    string addBinary(string a, string b) {

        if (a.size() < b.size()) {
            std::swap(a,b);
        }

        int la = a.size()-1;
        int lb = b.size()-1;
        int adder = 0;

        while(la >= 0) {
            adder += a[la] - '0';
            if (lb >= 0) {
                adder += b[lb--] - '0';
            }
            if (adder > 1) {
                a[la] = adder%2 + '0';
                adder = 1;
            } else {
                a[la] = adder + '0';
                adder = 0;
            }
            la--;
        }

        if (adder > 0) {
            a = '1' + a;
        }
        return a;
    }
};

